package com.example.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.example.common.support.ApiException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;

;

/**
 * @author sifan
 * @PackagName:com.example.common.utils
 * @ClassName: SmsUtils
 * @Description:
 * @date:2021/1/18 11:13
 */
@Slf4j
public final class SmsUtils {

    public static void sendSmsMessage(String phone, List<String> templateParamSet1, String templateId) {        
        String hostAddress = null;
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            hostAddress = localHost.getHostAddress();//获取IP地址
        }catch (Exception e){
        }

        if(hostAddress != null && (hostAddress.indexOf("192.168.1.") != -1 || hostAddress.indexOf("localhost") != -1||hostAddress.indexOf("192.168.182.") != -1)){
            return;
        }


       try {
           Credential cred = new Credential("AKIDrgGbpoLoytxlKnzFBrWyEG7oFQSImISV", "5vWktlNpSdYEH7i7HG4cv3ye7LJ6i4FE");

           HttpProfile httpProfile = new HttpProfile();
           httpProfile.setEndpoint("sms.tencentcloudapi.com");

           ClientProfile clientProfile = new ClientProfile();
           clientProfile.setHttpProfile(httpProfile);

           SmsClient client = new SmsClient(cred, "", clientProfile);

           SendSmsRequest req = new SendSmsRequest();
           String[] phoneNumberSet1 = {"+86" + phone};
           req.setPhoneNumberSet(phoneNumberSet1);

           req.setTemplateParamSet(templateParamSet1.toArray(new String[0]));

           req.setSign("重庆医盟健康");
           req.setSmsSdkAppid("1400194018");
           req.setTemplateID(templateId);

           SendSmsResponse resp = client.SendSms(req);
           String code = resp.getSendStatusSet()[0].getCode();
           String message = resp.getSendStatusSet()[0].getMessage();
           if (!"Ok".equals(code)) {
               throw new ApiException(1, message);
           }
           log.info(JSONObject.toJSONString(resp));
       } catch (TencentCloudSDKException e) {
           throw new ApiException(1, e.getMessage());
       }
    }
}
